package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import utils.AirTransportRepositoryManager;

public class NewFlightController extends AbstractController {

	private AirTransportRepositoryManager repositoryManager;
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String idParam = request.getParameter("flightID");
		String depTimeParam = request.getParameter("depTime");
		String arrTimeParam = request.getParameter("arrTime");
		String srcParam = request.getParameter("srcCity");
		String destParam = request.getParameter("destCity");
		String mscParam = request.getParameter("maxSeatsCount");

		ModelAndView mav = new ModelAndView();
		
		mav.addObject("cities", repositoryManager.getCities());
		
		if (idParam != null && depTimeParam != null &&
			arrTimeParam != null && mscParam != null &&
			idParam.length() > 0 && depTimeParam.length() > 0 &&
			arrTimeParam.length() > 0 && mscParam.length() > 0) {
			
			if ( repositoryManager.addNewFlight(idParam, depTimeParam, arrTimeParam, srcParam, destParam, mscParam) == true ) {
				mav.addObject("flightid", idParam);
				mav.addObject("flag", true);
			}
			else {
				mav.addObject("flightid", idParam);
				mav.addObject("flag", false);
			}
		}
		
		return mav;
	}

	public AirTransportRepositoryManager getRepositoryManager() {
		return repositoryManager;
	}

	public void setRepositoryManager(AirTransportRepositoryManager repositoryManager) {
		this.repositoryManager = repositoryManager;
	}


	
}
